package zcxn.microService.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

import zcxn.microService.bean.BattParamsInfo;
import zcxn.microService.bean.GlobalAllConfigInfo;
import zcxn.microService.bean.SPHParamsInfo;

public class MapDataToYaml {
	static final String globalPro = "/global.properties";
	/**
	 * 修改yaml文件[全局配置、储能参数、SPH参数]同时保存 并写出去
	 * 
	 * @param yamlpath
	 * @throws IOException
	 */
	public void updateYamlInfo(String gridId, GlobalAllConfigInfo global, BattParamsInfo batt,SPHParamsInfo sphParamsInfo) throws IOException {
		YamlPropertiesUtil propertiesUtil=new YamlPropertiesUtil();
		// 将properties文件转换为map
		Map<String, Object> map = propertiesUtil.loadProPerties(globalPro);
		// 将yaml文件转换为map结构
		Map<String, Object> configMap=new HashMap<String, Object>();
		HashMap<String, Object> globalInfo =new  HashMap<String, Object>() ;
		globalInfo.put("coll_freq", global.getCollFreq());
		globalInfo.put("soc_update_cycle", global.getSocUpdateCycle());
		globalInfo.put("cache_cycle", global.getCacheCycle());
		globalInfo.put("upload_retry_times",global.getUploadRetryTimes() );
		globalInfo.put("floating_volt_dev", global.getFloatingVoltDev());
		globalInfo.put("floating_hold_time",global.getFloatingHoldTime() );
		globalInfo.put("servicename", global.getServiceName());
		globalInfo.put("grid_id", global.getGridId());
		globalInfo.put("agentId", global.getAgentId());
		globalInfo.put("DATA_CHECK_TYPE", global.getDataCheckType());
		globalInfo.put("CHECK_BIT", global.getCheckBit());
		globalInfo.put("DATA_BIT", global.getDataBit());
		globalInfo.put("STOP_BIT",global.getStopBit() );
		globalInfo.put("TRANS_MODE", global.getTransMode());
		globalInfo.put("BAUD_RATE",global.getBaudRate());
		globalInfo.put("SLAVE_ADDRESS", global.getSlaveAddress());
		globalInfo.put("ELE_A_PORT", global.getEleAPort());
		globalInfo.put("ELE_INSTANCE_A_ID", global.getEleInstanceAId());
		globalInfo.put("ELE_INSTANCE_B_ID", global.getEleInstanceBId());
		globalInfo.put("REL_DIRECT", global.getRelDirect());
		globalInfo.put("BATT_ELE_INSTANCE_ID", global.getBattEleInstanceId());
		globalInfo.put("PV1_ELE_INSTANCE_ID", global.getPv1EleInstanceId());
		globalInfo.put("PV2_ELE_INSTANCE_ID", global.getPv2EleInstanceId());
		globalInfo.put("BACKUP_ELE_INSTANCE_ID", global.getBackupEleInstanceId());
		globalInfo.put("NORMAL_ELE_INSTANCE_ID", global.getNormalEleInstanceId());
		globalInfo.put("PCC_ELE_INSTANCE_ID", global.getPiccEleInstanceId());
		globalInfo.put("EDGE_ELE_INSTANCE_ID", global.getEdgeEleInstanceId());
		globalInfo.put("SPH_ELE_INSTANCE_ID", global.getSphEleInstanceId());
		globalInfo.put("SPH_PASSWORD", global.getSphPassword());
		globalInfo.put("SMART_MATER_ADDRESS", global.getSmartMaterAddress());
		globalInfo.put("EXTENNAL_SENSOR", global.getExtennalSensor());
		globalInfo.put("NEW_BATT",global.isNewBatt());
		globalInfo.put("MAX_UPLOAD_NUM", global.getMaxUploadNum());
		globalInfo.put("DATALOGGER_SPH_RS485", global.getDataLoggerSPHRS485());
		globalInfo.put("DATA_TOUPLOAD", global.getDataToupload());
		globalInfo.put("REPORT_URL", global.getReportUrl());
		globalInfo.put("FLOAT_TIMES", global.getFloatTimes());
		globalInfo.put("CLEAR_UPLOAD_DATA_AGO", global.getClearUploadDataAgo());
//		储能参数
		HashMap<String, Object> esuInfo = new HashMap<String, Object> ();
		// 从电池参数中获取
		esuInfo.put("ELE_INSTANCE_ID", batt.getEleInstanceId());
		esuInfo.put("DISC_STOP_V", batt.getDiscStopV() * 10);
		esuInfo.put("CHARGE_RATIO", batt.getChangeRatio());
		esuInfo.put("CYCLE_V", batt.getCycleV() * 10);
		esuInfo.put("FLOAT_V", batt.getFloatV() * 10);
		esuInfo.put("OVER_V_PROTECT", batt.getOverVProtect());
		esuInfo.put("UNDER_V_PROTECT", batt.getUnderVProtect());
		esuInfo.put("BAT_OVER_V_PROTECT", batt.getBatOverVProtect());
		esuInfo.put("BAT_UNDER_V_PROTECT", batt.getBatUnderVProtect());
		esuInfo.put("DISC_DOD", batt.getDiscDOD());
		esuInfo.put("CHARGE_STOP_A", batt.getChangeStopA());
		esuInfo.put("BAT_TYPE", batt.getBatType());
		esuInfo.put("BAT_GROUP_NUM", batt.getBatGroupNum());
		esuInfo.put("BAT_VOLUME", batt.getBatVolume());
		esuInfo.put("MAX_CHARGE_A", batt.getMaxChangeA());
		esuInfo.put("MAX_DISC_A", batt.getMaxDiscA());
		esuInfo.put("MAX_CHARGE_W", batt.getMaxChangeW());
		esuInfo.put("MAX_DISC_W", batt.getMaxDiscW());
		esuInfo.put("RUNTIME_BATT_SOC", batt.getRuntimeBattSoc());
		esuInfo.put("RUNTIME_BATT_SOH",batt.getRuntimeBattSoh() );
		esuInfo.put("DUMP_ENERGY", batt.getDumpEnergy());
		esuInfo.put("CHARGE_EFFIC", batt.getChargeEffic());
		esuInfo.put("BAT_RATE_V", batt.getBatRateV());
		esuInfo.put("BAT_TOTAL_DISCHARGE_QUANITY",batt.getBatTotalDischargeQuanity());
		esuInfo.put("BAT_TOTAL_CHARGE_QUANITY",batt.getBatTotalChargeQuanity());
		esuInfo.put("BAT_CHANGE_QUANITY",batt.getBatChangeQuanity() );
		esuInfo.put("CHARGE_EFFIC",batt.getChargeEffic());
		System.out.println(globalInfo);
		System.out.println(esuInfo);
		HashMap<String, Object> sphInfo = new HashMap<String, Object> ();
		sphInfo.put("ELE_INSTANCE_ID", sphParamsInfo.getEleInstanceId());
		sphInfo.put("RUNTIME_INV_MOD", 0);
		sphInfo.put("MAX_ACTIVE_POWER", sphParamsInfo.getMaxActivePower());
		sphInfo.put("INIT_SPH_PF", sphParamsInfo.getInitSPHPf());
		sphInfo.put("INIT_REACTIVE_POWER", sphParamsInfo.getInitReactivePower());
		sphInfo.put("REVERSE_FLOW", getValToInt(sphParamsInfo.getReverseFlow()));
		sphInfo.put("MAX_GRID_UPS_POWER", sphParamsInfo.getMaxGridUpsPower());
		sphInfo.put("ACTIVE_POWER_START_RATE", sphParamsInfo.getActivePowerStartRate());
		sphInfo.put("ACTIVE_ISLAND", getValToInt(sphParamsInfo.getActiveIsland()));
		sphInfo.put("STANDARD_TYPE", sphParamsInfo.getStandardType());
		sphInfo.put("BAT_CHARG_DISCHARG_POWER", sphParamsInfo.getBatChargDischargPower());
		sphInfo.put("OUTER_CONTROL_MODEL", getValToInt(sphParamsInfo.getOuterControlModel()));
		sphInfo.put("POWER_CONTROL_MODEL", getValToInt(sphParamsInfo.getPowerControlModel()));
		sphInfo.put("GRID_CONTROL_POWER", sphParamsInfo.getGridControlModel());
		sphInfo.put("GRID_RECOVER_TIME", sphParamsInfo.getGridRecoverTime());
		
		Map modbusMap = null;
		Yaml yamlFile = new Yaml();
		URL url = MapDataToYaml.class.getClassLoader().getResource("modbusComm.yaml");
		File ymlFile = new File(url.getPath());
		if (ymlFile != null && ymlFile.exists()) {
			modbusMap = (Map) yamlFile.load(new FileInputStream(ymlFile));
		 }
		System.out.println(modbusMap.get("CCF"));
		System.out.println(modbusMap.get("DI"));
		
		// 将修改后的map 存放在yaml 文件转换的map 中
		configMap.put("GLOBAL", globalInfo);
		configMap.put("ESU", esuInfo);
		configMap.put("ESI", sphInfo);
		configMap.put("CCF", modbusMap.get("CCF"));
		configMap.put("DI", modbusMap.get("DI"));
		
		
	
		// 将新的的map 写入到文件中
		DumperOptions dumperOptions = new DumperOptions();
		dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
		Yaml yaml = new Yaml(dumperOptions);
		File dumpFile = new File("e:/PackageMicroService/".concat(global.getServiceName()).concat("/Config").concat(global.getGridId()+"").concat(".yaml"));
		if (!dumpFile.getParentFile().exists()) {
			dumpFile.getParentFile().mkdirs();
		}
		FileWriter fileWriter = new FileWriter(dumpFile);
		yaml.dump(configMap, fileWriter);
		fileWriter.close();
		
		
	}

	public static int getValToInt(String onOrOff) {
		if (onOrOff.equals("ON")){
			return 0;
		}else {
			return 65280;
		}
	}
	

}
